package com.smart.six.event;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;

/**
 * 邮件发送者
 *
 * @author junrui.wang
 * @create 2018/7/18
 */
//MailSender要想有发布事件的能力，必须实现ApplicationContextAware接口
public class MailSender implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    //ApplicationContextAware接口方法，以便容器启动时注入容器实例
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void sendMail(String to){
        System.out.println("MailSender:模拟发送邮件。。。");
        MailSendEvent mailSendEvent = new MailSendEvent(applicationContext, to);
        //向容器中的所有事件监听器发送事件
        applicationContext.publishEvent(mailSendEvent);
    }
}
